package com.xinggq.user.service.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

/**
 * 角色权限关联实体类
 *
 * @author system
 * @since 2025/1/10
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_role_permission")
public class SysRolePermissionEntity {

    /**
     * 关联ID，使用分布式ID生成器
     */
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;

    /**
     * 角色ID
     */
    private Long roleId;

    /**
     * 权限ID
     */
    private Long permissionId;

    /**
     * 授权人ID
     */
    private Long grantedBy;

    /**
     * 授权时间
     */
    private LocalDateTime grantedAt;

    /**
     * 状态：1-正常，0-禁用
     */
    private Integer status;

    /**
     * 设置授权信息
     */
    public void setGrantInfo(Long granterId) {
        LocalDateTime now = LocalDateTime.now();
        this.grantedAt = now;
        this.grantedBy = granterId;
        if (this.status == null) {
            this.status = 1; // 默认正常状态
        }
    }

    /**
     * 检查关联是否有效
     */
    public boolean isValid() {
        return this.status != null && this.status == 1;
    }
}